package com.example.onlinetaxi.ui.activity.MainPage.MineActivities;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MineChangePasswordActivity extends BaseActivity {
    // 组件
    @BindView(R.id.mine_cancel_change_password)
    TextView mineCancelBtn;
    @BindView(R.id.mine_confirm_change_password)
    TextView mineConfirmBtn;
    @BindView(R.id.mine_modify_password)
    TextView minePassword;
    @BindView(R.id.mine_modify_repassword)
    TextView mineRepassword;
    @BindView(R.id.mine_modify_new_password)
    TextView mineNewPassword;

    // 用户输入
    private String password;
    private String repassword;
    private String newPassword;

    // 参数
    private Map<String, String> map = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_change_password);
        // 使用@BindView必须要有！
        ButterKnife.bind(this);

        initEvent();
    }

    private void initEvent() {
        mineCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消保存，关闭当前界面
                finish();
            }
        });

        mineConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 同意保存
                // 获取用户输入
                if (getUserInput()) {
                    saveUserInfo();
                }

            }
        });
    }

    private Boolean getUserInput() {
        password = minePassword.getText().toString();
        repassword = mineRepassword.getText().toString();
        newPassword = mineNewPassword.getText().toString();

        if (password == null || repassword == null || newPassword == null
                || password.equals("") || repassword.equals("") || newPassword.equals("")) {
            Toast.makeText(this, "修改密码失败：输入项不能为空！请检查您的输入", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!password.equals(repassword)) {
            Toast.makeText(this, "修改密码失败：您输入的两次密码不一致！", Toast.LENGTH_SHORT).show();
            return false;
        } else if (password.equals(newPassword)) {
            Toast.makeText(this, "修改密码失败：您的新密码与旧密码一致！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void saveUserInfo() {
        map.clear();
        map.put("phone", LocalStorage.phone);
        map.put("password", password);
        map.put("newPassword", newPassword);

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "user", "changePassword");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (!dataResponse.getCode().equals("200")) {
            Toast.makeText(this, "修改密码失败：" + dataResponse.getData().get("error").toString(), Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(MineChangePasswordActivity.this, "密码修改成功！", Toast.LENGTH_SHORT).show();

            finish();
        }
    }
}